import MarkdownIt from 'markdown-it';

function autoLineBreakPlugin(md: MarkdownIt) {
    const defaultRender = md.renderer.rules.paragraph_open || function (tokens, idx, options, env, self) {
        return self.renderToken(tokens, idx, options);
    };

    md.core.ruler.after('inline', 'auto_line_break', state => {
        const tokens = state.tokens;
        for (let i = 0; i < tokens.length; i++) {
            console.log("tokens[i]", tokens[i])
            if (tokens[i].type === 'inline' && tokens[i + 1] && tokens[i + 1].type === 'paragraph_close') {
                const content = tokens[i].content;

                // 使用正则表达式匹配 "数字. 空格" 的模式
                const match = content.match(/^(\d+)\.\s/);

                if (match) {
                    // 将匹配到的部分替换为带有 <br> 标签的内容
                    tokens[i].content = content.replace(/^(\d+)\.\s/, `<strong>${match[1]}.</strong> `);
                }
            }
        }
    });

    md.renderer.rules.paragraph_open = (tokens, idx, options, env, self) => {
        return defaultRender(tokens, idx, options, env, self);
    };
}

export default autoLineBreakPlugin;
